package com.example.pilipili.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.pilipili.common.R;
import com.example.pilipili.pojo.entity.Collection;
import com.example.pilipili.pojo.entity.Follow;
import com.example.pilipili.dao.FollowMapper;
import com.example.pilipili.service.FollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liyv
 * @since 2023-12-01
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements FollowService {

    @Autowired
    FollowMapper followMapper ;
    @Override
    public R<Follow> click(Follow entry) {
        QueryWrapper<Follow> qw = new QueryWrapper<>() ;
        qw.eq("f_ing",entry.getfIng()).eq("f_ed",entry.getfEd());

        if( !followMapper.exists(qw)) {
            entry.setTime(LocalDate.now());
            followMapper.insert(entry) ;
            return R.ok(entry) ;
        }
        followMapper.delete(qw) ;
        return R.ok();
    }

    @Override
    public boolean isFollow(int ing, int ed) {
        QueryWrapper<Follow> qw = new QueryWrapper<>() ;
        qw.eq("f_ing",ing).eq("f_ed",ed);
        return followMapper.exists(qw);
    }

    @Override
    public int UFIngNums(int uid) {
        QueryWrapper<Follow> qw = new QueryWrapper<>() ;
        qw.eq("f_ing",uid);
        return Math.toIntExact(followMapper.selectCount(qw));
    }

    @Override
    public int UFEdNums(int uid) {
        QueryWrapper<Follow> qw = new QueryWrapper<>() ;
        qw.eq("f_ed",uid);
        return Math.toIntExact(followMapper.selectCount(qw));
    }



}
